Namespace PeriodProcessing

    ''' <summary>
    ''' For DAS processing, we also process PAS satisfied requierments 
    ''' </summary>
    ''' <remarks></remarks>
    Public Class CertifSatisfiedReqsProviderDAS
        Inherits CertifSatisfiedReqsProvider

        ''' <summary>
        ''' Class used to hold results from PAS_HISTORY sql below
        ''' </summary>
        Private Class PASStudentHistory

            Public ClassMemberId As Integer?
            Public StudentLessonExceptionId As Integer?
            Public LessonId As Integer

            Public Overrides Function Equals(ByVal obj As Object) As Boolean

                If obj Is Nothing Then Return False
                If TypeOf obj Is PASStudentHistory = False Then Return False

                If CType(obj, PASStudentHistory).LessonId = Me.LessonId Then Return True

                Return False

            End Function

        End Class


        Public Overrides Function getStudentCertificationSatisfiedRequirement( _
                    ByVal processPeriod As Period, _
                    ByVal certificationType As CertificationType, _
                    ByVal student As Student) As List(Of CertificationSatisfiedRequirement)

            Dim ret As List(Of CertificationSatisfiedRequirement) = _
                        MyBase.getStudentCertificationSatisfiedRequirement(processPeriod, _
                                                                           certificationType, _
                                                                           student)

            If student.MemberNumber = PeriodProcessor.TRACK_STUDENT_NUMBER Then
                Trace.TraceInformation("Loading from PAS history for student {0}", _
                                       student.MemberNumber)
            End If

            'For DAS, also create CertificationSatisfiedRequirements from PAS
            Dim PAS_HISTORY = <sql>
SELECT a.ClassMemberId, a.StudentLessonExceptionId , a.lessonid
FROM vGridStudentExamCreditHistory a WHERE a.studentid={0} 
and ((a.historytype='EXAM' and a.Grade>=a.PassGrade) or historytype='CREDIT')
and a.periodDate &lt; '2009-06-01' -- this will only return old pas records, since 1/6/09 is the cutoff date
and a.lessonid in (select lessonid from vCertificationLesson where CertificationTypeId={1} ) 
order by periodDate desc</sql>

            Dim rs As Data.IDataReader = Nothing
            Dim pasHistory As New List(Of PASStudentHistory)

            Try
                rs = ModelContext.CurrentDBUtils.getDataReaderWithParams(PAS_HISTORY.Value, _
                                    student.StudentId, _
                                    certificationType.CertificationTypeId)

                While rs.Read
                    Dim p As New PASStudentHistory

                    If rs.IsDBNull(0) = False Then
                        p.ClassMemberId = rs.GetInt32(0)
                    End If
                    If rs.IsDBNull(1) = False Then
                        p.StudentLessonExceptionId = rs.GetInt32(1)
                    End If
                    If rs.IsDBNull(2) = False Then
                        p.LessonId = rs.GetInt32(2)
                    End If

                    If Not pasHistory.Contains(p) Then
                        pasHistory.Add(p)
                    End If


                End While

            Finally
                ModelContext.CurrentDBUtils.closeDataReader(rs)
            End Try

            
            For Each historyRec As PASStudentHistory In pasHistory

                Dim lesson As Lesson = LessonDataUtils.findByKey(historyRec.LessonId)
                Dim lessonReqs As IEnumerable(Of CertificationRequirement) = _
                        lesson.CertificationRequirementsOfCertificationType(certificationType)

                For Each dreq In lessonReqs
                    Dim csr As CertificationSatisfiedRequirement = CertificationSatisfiedRequirementFactory.Create
                    csr.CertificationRequirementId = dreq.CertificationRequirementId

                    If historyRec.ClassMemberId Is Nothing Then
                        Dim sleu As StudentLessonCreditUsage = StudentLessonCreditUsageFactory.Create
                        sleu.StudentLessonExceptionId = historyRec.StudentLessonExceptionId
                        csr.CreditUsage = sleu
                        csr.ClassMember = Nothing
                    Else
                        csr.CreditUsage = Nothing
                        csr.ClassMember = ClassMemberDataUtils.findByKey(CInt(historyRec.ClassMemberId))
                    End If

                    If student.MemberNumber = PeriodProcessor.TRACK_STUDENT_NUMBER Then
                        Trace.TraceInformation("from PAS, lesson:{0}", lesson.LessonCode)
                    End If

                    ret.Add(csr)
                Next
            Next

            If student.MemberNumber = PeriodProcessor.TRACK_STUDENT_NUMBER Then
                Trace.TraceInformation("Loaded from PAS periods {0} records", ret.Count)
            End If
            Return ret

        End Function
    End Class
End Namespace
